package cn.virens.packet.handler;

import org.tio.core.ChannelContext;

import cn.virens.App;
import cn.virens.TioUtil;
import cn.virens.app.common.SQL;
import cn.virens.packet.ReadPacket;
import cn.virens.packet.WritePacket;

public class HeartbeatHandler extends AbstractHandler {

	@Override
	public void read(ReadPacket packet, ChannelContext channelContext) {
		logger.debug("接收心跳包");
		TioUtil.heartTimes(channelContext, (t) -> t.incrementAndGet());
		TioUtil.heartIdle(channelContext, (t) -> t.getAndSet(0));

		String imsi = TioUtil.imei(channelContext);//imei
		// 将数据存入数据库
		App.db().executeUpdate(SQL.HEARTBEAT, imsi);

		//构建应答包数据
		WritePacket writePacket = WritePacket.of(packet, "8001");
		//发送应答包
		TioUtil.write(channelContext, writePacket);
	}
}
